package com.huatao.subject.domain.handler.subject;

import com.huatao.subject.common.enums.IsDeleteFlag;
import com.huatao.subject.common.enums.SubjectInfoTypeEnum;
import com.huatao.subject.domain.convert.RadioSubjectConverter;
import com.huatao.subject.domain.entity.SubjectInfoBO;
import com.huatao.subject.domain.entity.SubjectOptionBO;
import com.huatao.subject.infra.basic.entity.SubjectRadio;
import com.huatao.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectRadio> subjectRadioList = new LinkedList<>();
        if (subjectInfoBO.getOptionLists() != null) {
            subjectInfoBO.getOptionLists().forEach(option -> {
                SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.convertAnswerToRadio(option);
                subjectRadio.setSubjectId(subjectInfoBO.getId());
                subjectRadio.setIsDeleted(IsDeleteFlag.NOT_DELETED.getCode());
                subjectRadioList.add(subjectRadio);
            });
        }
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        return null;
    }
}
